﻿using Kinlo.MESDocking;
using NPOI.OpenXmlFormats.Spreadsheet;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using UIWindows.Models.Devices;
using XingLucifer.Devices;
using static UIWindows.Services.PLC_MESHelper.RowPlateFinishedHandler;

namespace UIWindows.Services.PLC_MESHelper
{
    /// <summary>
    /// 预充前扫码
    /// </summary>
    public class BeforeScanBarcodeHandler : HelperBase
    {
        PLCBase InjMachine { get; set; }
        MESWinderInRecord _mesWinderInRecord { get; set; }
        public BeforeScanBarcodeHandler(
            DeviceInteractiveaddressStatic deviceInteractiveaddressStatic,
            DeviceClientModel deviceModel,
            TemporaryStatic temporaryStatic,
            ParameterStatic parameterStatic,
            DeviceConfigStatic deviceConfigStatic,
            SystemConfigStatic systemConfigStatic,
             HomeStatic homeStatic,
             Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog
            ) : base(deviceInteractiveaddressStatic, deviceModel, temporaryStatic, parameterStatic, deviceConfigStatic, systemConfigStatic, homeStatic, log, dialog)
        {
            InjMachine = deviceModel.PLC;
            _mesWinderInRecord = new MESWinderInRecord(_log, _dialog);
        }
        public override Task Handle()
        {
            //KL21050011Machine InjMachine = null;
            Int16 testResult = 2;
            Int16 battId = 0;
            //List<KNCScannerMachine> Scanner = null;
            ushort trayNo = 0;
            try
            {
                //var dicts = ctx.Container.Get<ConcurrentDictionary<string, object>>();
                //var cfg = ctx.Container.Get<CFG>();
                //InjMachine = (KL21050011Machine)dicts.GetOrAdd("InjMachine", InjMachine);
                //Scanner = (List<KNCScannerMachine>)dicts.GetOrAdd("BeforeScaner", Scanner);
                //var DB = ctx.Container.Get<RepoBase>();
                //var Counter = ctx.Container.Get<Counter>();
                //var data = ctx.Container.Get<PlateData>();
                 RunLog($"【前扫码】启动接收");
                var barcode = "";
                // Scanner?.FirstOrDefault().StartScan();
                // barcode = Scanner?.FirstOrDefault().Receive().Trim();
                //var barcode = CoreService.BeforeBarcode;
                // CoreService.UpdateBarcode("");
                trayNo = InjMachine.Read("DM1001", 1)[0];
                barcode = InjMachine.ReadString("DM19000", 40).Trim();
                //lock (locker)
                //{
                //    if (data.TrayNo >= 9999)
                //        data.TrayNo = 1;
                //    trayNo = data.TrayNo++;
                //}
                if (string.IsNullOrEmpty(barcode))
                {
                    barcode = DateTime.Now.ToString("yyyyMMddHHmmss");
                }
               RunLog($"【前扫码】托架号:{trayNo} 条码:{barcode} ");

                string barcode1 = System.Text.RegularExpressions.Regex.Match(barcode, _systemConfig.SCM.BarcodeRules, System.Text.RegularExpressions.RegexOptions.Compiled).Value;
                if (!string.IsNullOrEmpty(barcode1))
                {
                    //data.ScanQueueList.Add(new PlateBattery { ID = (ushort)trayNo, Barcode = barcode });
                    battId = 0;
                    testResult = 1;
                }
                else
                {
                    testResult = 2;
                }
            }
            catch (Exception ex)
            {
                testResult = 2;
                // trayNo = 3000;
                RunLog($"【前扫码】发生异常:{ex}");
            }
            finally
            {
                // var r0 = InjMachine.PLC.Write<short>("DM1210", new short[] { trayNo });
                //  var r1 = InjMachine.PLC.Write<short>("DM1201", new short[] { testResult });
                var r2 = InjMachine.Write<short>("DM1200", new short[] { testResult });
                var r3 = InjMachine.Write<short>("DM1000", new short[] { 0 });
                RunLog($"【前扫码】写结果信号 {testResult} {r2}.{r3} {(r2 && r3 ? "成功" : "失败")}");
            }
            return base.Handle();
        }
    }
}
